﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HF.BaseWebCtrlLib;
using HF.Framework.DataClient;
using HF.Framework.DataClientDBAgent;
public partial class UserControls_PageBarTwo : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (PageSql != "")
                GridViewBind();

            if (AefterQueryBind != null)
                AefterQueryBind(this, EventArgs.Empty);
        }

        if (GridView != null)
            EmptyRecordControl();
        tablePanel.Width = width;
    }

    public event EventHandler AefterQueryBind;

    #region 分页控制
    public void GridViewBind()
    {
        HttpContext.Current.Response.Cookies.Add(new HttpCookie("PageSql", Server.UrlEncode(PageSql.ToString())));
        if (DoubleGridView == true)
        {
            HttpContext.Current.Response.Cookies.Add(new HttpCookie(GridView.ID + "PageIndex", PageStart.ToString()));
        }
        else
        {
            HttpContext.Current.Response.Cookies.Add(new HttpCookie("PageIndex", PageStart.ToString()));
        }
        QueryResult query=null;
       ClientDBAgent agent=new ClientDBAgent();
        if (XmlFileName != "")
        {
         
           // query = agent.GetTable(PageSql, PageStart, PageSize, XmlFileName);
        }
        else
        {
           // query = agent.GetTable(PageSql, PageStart, PageSize);
        }

        RecordCount = query.Total;

        DataTable table = query.DataView;

        if (DataList != null)
        {
            DataList.DataSource = table;
            DataList.DataBind();
            Show();
        }

        if (GridView != null)
        {
            CurrPageRecordCount = table.Rows.Count;
            for (int i = 0; i < PageSize - CurrPageRecordCount; i++)
            {
                DataRow row = table.NewRow();
                for (int j = 0; j < table.Columns.Count; j++)
                {
                    if (!table.Columns[j].AllowDBNull)
                    {
                        try
                        {
                            row[j] = "";
                        }
                        catch
                        {
                            row[j] = 0;
                        }
                    }
                }
                table.Rows.Add(row);
            }

            GridView.DataSource = table;
            GridView.DataBind();
            Show();
            EmptyRecordControl();
        }
    }

    public void EmptyRecordControl()
    {
        for (int index = 0; index < GridView.Rows.Count; index++)
        {
            GridViewRow row = GridView.Rows[index];

            if (index < CurrPageRecordCount)
            {
                row.Attributes.Add("onmouseout", "mouseout(this)");
                row.Attributes.Add("onmouseover", "this.className = 'GridView_Row_MouseOver'");
                row.Attributes.Add("onclick", string.Format("Click('{0}', this, {1})", txtRowIndex.ClientID, index));
            }
            else
            {
                for (int i = 0; i < row.Cells.Count; i++)
                {
                    row.Cells[i].Controls.Clear();
                    Label lbl = new Label();
                    lbl.Text = "";
                    row.Cells[i].Controls.Add(lbl);
                }
            }
        }
    }

    public void Show()
    {
        lblCurrPage.Text = PageIndex().ToString();
        lblTotalPage.Text = PageCount().ToString();
        lblTotalCount.Text = RecordCount.ToString();
        btnFirst.Enabled = (PageCount() > 1 && PageIndex() > 1);
        btnPrior.Enabled = PageIndex() > 1;
        btnNext.Enabled = PageIndex() < PageCount();
        btnEnd.Enabled = (PageCount() > 1 && PageIndex() < PageCount());
    }

    private int FinalStart()
    {
        if (PageSize == 0)
        {
            return 0;
        }
        else
        {
            int finalPageSize = RecordCount % PageSize;
            return RecordCount - (finalPageSize == 0 ? PageSize : finalPageSize);
        }
    }

    private int PageIndex()
    {
        // 因为PageStart始终等于PageSize * i + 1，所以始终不能整除PageSize，所以PageStart/PageSize并直接加 1.
        return PageSize == 0 ? 0 : (PageStart / PageSize + 1);
    }

    private int PageCount()
    {
        // 当RecordCount能整除PageSize或刚出头不能整除时，PageCount不相同，所以使用Math.Sign分是否能整除的情况处理.
        return PageSize == 0 ? 0 : (RecordCount / PageSize + Math.Sign(RecordCount % PageSize));
    }
    #endregion

    #region 属性

    private string width;
    /// <summary>
    /// 控件宽度
    /// </summary>
    public string Width
    {
        get
        {
            return width == "" ? "100%" : width;
        }
        set
        {
            width = value;
        }
    }

    /// <summary>
    /// 判断该页面是否有多个GridView
    /// </summary>
    public bool DoubleGridView
    {
        get
        {
            return ViewState["DoubleGridView"] == null ? false : (bool)ViewState["DoubleGridView"];
        }
        set
        {
            ViewState["DoubleGridView"] = value;
        }
    }

    private GridView _GridView;

    public GridView GridView
    {
        get
        {
            return _GridView;
        }
        set
        {
            _GridView = value;
        }
    }

    private DataList _DataList;

    public DataList DataList
    {
        get
        {
            return _DataList;
        }
        set
        {
            _DataList = value;
        }
    }

    public int RecordCount
    {
        get
        {
            return ViewState["RecordCount"] == null ? 0 : (int)ViewState["RecordCount"];
        }
        set
        {
            ViewState["RecordCount"] = value;
        }
    }

    public int CurrPageRecordCount
    {
        get
        {
            return ViewState["CurrPageRecordCount"] == null ? 0 : (int)ViewState["CurrPageRecordCount"];
        }
        set
        {
            ViewState["CurrPageRecordCount"] = value;
        }
    }

    public int PageStart
    {
        get
        {
            return ViewState["PageStart"] == null ? 0 : (int)ViewState["PageStart"];
        }
        set
        {
            ViewState["PageStart"] = value;
        }
    }

    public int PageSize
    {
        get
        {
            return ViewState["PageSize"] == null ? 0 : (int)ViewState["PageSize"];
        }
        set
        {
            ViewState["PageSize"] = value;
        }
    }

    public string PageSql
    {
        get
        {
            return ViewState["PageSql"] == null ? string.Empty : (string)ViewState["PageSql"];
        }
        set
        {
            ViewState["PageSql"] = value;
        }
    }

    public string XmlFileName
    {
        get
        {
            return ViewState["XmlFileName"] == null ? string.Empty : (string)ViewState["XmlFileName"];
        }
        set
        {
            ViewState["XmlFileName"] = value;
        }
    }
    #endregion

    #region 按钮事件
    protected void btnFirst_Click(object sender, EventArgs e)
    {
        PageStart = 0;
        GridViewBind();
    }

    protected void btnPrior_Click(object sender, EventArgs e)
    {
        PageStart = (Math.Max(0, PageStart - PageSize));
        GridViewBind();
    }

    protected void btnNext_Click(object sender, EventArgs e)
    {
        PageStart = (Math.Min(FinalStart(), PageStart + PageSize));
        GridViewBind();
    }

    protected void btnEnd_Click(object sender, EventArgs e)
    {
        PageStart = (FinalStart());
        GridViewBind();
    }

    protected void btnGo_Click(object sender, EventArgs e)
    {
        int pageIndex = 0;
        try
        {
            pageIndex = Convert.ToInt32(txtTarget.Text);
        }
        catch
        {
        }
        if (pageIndex > 0 && pageIndex <= PageCount())
        {
            PageStart = (pageIndex - 1) * PageSize;
            GridViewBind();
        }
    }
    #endregion
}
